package com.dmx.cep.action.attributeManage;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.dmx.cep.constant.ConfAttributeConstant;
import com.dmx.cep.entity.ConfAttributeSelection;
import com.dmx.cep.service.ConfAttributeSelectionService;
import com.dmx.fw.constant.ViewConstant;
import com.dmx.fw.web.struts2.action.CommonQueryAction;





/**
 *
 * @author Alex.lu
 *
 */
@SuppressWarnings("serial")
public class ConfAttributeSelectionAction extends CommonQueryAction {

    private static final String BATCH_DELETE = "batchDelete";
	private ConfAttributeSelectionService confAttributeSelectionService;
	private ConfAttributeSelection confAttributeSelection;

	private Integer[] ids;
	private Integer id;


    private String q_confDescription;


    private String q_confKey;


    private String q_confName;


    private String q_confValue;
    
    private Integer usePreKeyUse;

    private Integer usePostKeyUse;
    
    private Integer usePostDomainUse;
    
    private Integer epgExpireDay;

    
    
    private Integer apiSearchUse;
    private Integer twitterApiSearchUse;

    

	// actions begin



	public String execute() throws Exception {
		super.busiId = "confAttributeSelectionSearch";
		if (ViewConstant.QUERY.equals(super.act)) {
			doQuery();
		} else if (ViewConstant.REQUERY.equals(super.act)) {
			doReQuery();
		} else if (ViewConstant.DELETE.equals(super.act)) {
			this.doDelete();
		} else if (BATCH_DELETE.equals(super.act)) {
			doBatchDelete();
		} else if (ViewConstant.ADDINPUT.equals(super.act)) {
			this.pageState = ViewConstant.ADDSTATUS;
			return doAddInput();
		} else if (ViewConstant.ADDSUBMIT.equals(super.act)) {
			return doAddSubmit();
		} else if (ViewConstant.EDITINPUT.equals(super.act)) {
			this.pageState = ViewConstant.EDITSTATUS;
			return doEditInput();
		} else if (ViewConstant.EDITSUBMIT.equals(super.act)) {
			return doEditSubmit();
		}
		return SUCCESS;
	}

	public String doAddInput() {
		this.pageState = ViewConstant.ADDSTATUS;
		return ViewConstant.ADDINPUT;
	}

	public String doAddSubmit() throws Exception {

//		if (haserrors) {
//			this.setErrorInfo(getText("m3p.asset.distribution.policy.expression.warning"));
//			return ViewConstant.ADDINPUT;
//		}

        /**do your add logics**/
		if(confAttributeSelectionService.isExistConfAttributeSelectionName(confAttributeSelection.getConfName())){
			super.setErrorInfo(getText("info.addFail"));
			super.setErrorInfo(getText("cep.confAttributeSelection.confName.duplicate"));
			this.pageState = ViewConstant.ADDSTATUS;
			return ViewConstant.ADDINPUT;
		}
		confAttributeSelectionService.saveOrUpdateConfAttributeSelection(confAttributeSelection);
		setSuccessinfo(getText("info.addSuccess"));
		super.doReQuery();
		return SUCCESS;
	}
	public String doEditInput() {

			this.pageState = ViewConstant.EDITSTATUS;
			ConfAttributeSelection tempConfAttributeSelection;
			Map<String,ConfAttributeSelection> confAttributeSelections = confAttributeSelectionService.findConfAttributeSelectionKeyMap();
			tempConfAttributeSelection = confAttributeSelections.get(ConfAttributeConstant.KEY_USE_PRE_KEY_BLACK);
			if(tempConfAttributeSelection!=null&&tempConfAttributeSelection.getConfValue()==1){
				usePreKeyUse=1;
			}
			
			tempConfAttributeSelection = confAttributeSelections.get(ConfAttributeConstant.KEY_USE_PRE_KEY_WHITE);
			if(tempConfAttributeSelection!=null&&tempConfAttributeSelection.getConfValue()==1){
				usePreKeyUse=2;
			}
			tempConfAttributeSelection = confAttributeSelections.get(ConfAttributeConstant.KEY_NO_USE_PRE_KEY);
			if(tempConfAttributeSelection!=null&&tempConfAttributeSelection.getConfValue()==1){
				usePreKeyUse=3;
			}
			
			tempConfAttributeSelection = confAttributeSelections.get(ConfAttributeConstant.KEY_USE_POST_KEY_BLACK);
			if(tempConfAttributeSelection!=null&&tempConfAttributeSelection.getConfValue()==1){
				usePostKeyUse=1;
			}
			
			tempConfAttributeSelection = confAttributeSelections.get(ConfAttributeConstant.KEY_USE_POST_KEY_WHITE);
			if(tempConfAttributeSelection!=null&&tempConfAttributeSelection.getConfValue()==1){
				usePostKeyUse=2;
			}
			tempConfAttributeSelection = confAttributeSelections.get(ConfAttributeConstant.KEY_NO_USE_POST_KEY);
			if(tempConfAttributeSelection!=null&&tempConfAttributeSelection.getConfValue()==1){
				usePostKeyUse=3;
			}
			
			tempConfAttributeSelection = confAttributeSelections.get(ConfAttributeConstant.KEY_USE_POST_DOMAIN_BLACK);
			if(tempConfAttributeSelection!=null&&tempConfAttributeSelection.getConfValue()==1){
				usePostDomainUse=1;
			}
			
			tempConfAttributeSelection = confAttributeSelections.get(ConfAttributeConstant.KEY_USE_POST_DOMAIN_WHITE);
			if(tempConfAttributeSelection!=null&&tempConfAttributeSelection.getConfValue()==1){
				usePostDomainUse=2;
			}
			tempConfAttributeSelection = confAttributeSelections.get(ConfAttributeConstant.KEY_NO_USE_POST_DOMAIN);
			if(tempConfAttributeSelection!=null&&tempConfAttributeSelection.getConfValue()==1){
				usePostDomainUse=3;
			}
			
			tempConfAttributeSelection = confAttributeSelections.get(ConfAttributeConstant.KEY_USE_BAIDU_API);
			if(tempConfAttributeSelection!=null&&tempConfAttributeSelection.getConfValue()==1){
				apiSearchUse = 1;
			}
			
			tempConfAttributeSelection = confAttributeSelections.get(ConfAttributeConstant.KEY_USE_GOOGLE_API);
			if(tempConfAttributeSelection!=null&&tempConfAttributeSelection.getConfValue()==1){
				apiSearchUse = 2;
			}
			
			tempConfAttributeSelection = confAttributeSelections.get(ConfAttributeConstant.KEY_USE_YAHOO_API);
			if(tempConfAttributeSelection!=null&&tempConfAttributeSelection.getConfValue()==1){
				apiSearchUse = 3;
			}
			tempConfAttributeSelection = confAttributeSelections.get(ConfAttributeConstant.KEY_USE_SINA_API);
			if(tempConfAttributeSelection!=null&&tempConfAttributeSelection.getConfValue()==1){
				twitterApiSearchUse = 1;
			}
			tempConfAttributeSelection = confAttributeSelections.get(ConfAttributeConstant.KEY_USE_TWITTER_API);
			if(tempConfAttributeSelection!=null&&tempConfAttributeSelection.getConfValue()==1){
				twitterApiSearchUse = 2;
			}
			tempConfAttributeSelection = confAttributeSelections.get(ConfAttributeConstant.KEY_EPG_EXPIRE_DAY);
			if(tempConfAttributeSelection!=null){
				epgExpireDay = tempConfAttributeSelection.getConfValue();
			}


		return ViewConstant.ADDINPUT;
	}

	public String doEditSubmit() throws Exception {


//		if (someerrorcondition) {
//			super.setErrorInfo(getText("m3p.asset.distribution.policy.required.warning"));
//			this.pageState = "edit";
//			return ViewConstant.ADDINPUT;
//		}
		ConfAttributeSelection tempConfAttributeSelection;
		Map<String,ConfAttributeSelection> confAttributeSelections = confAttributeSelectionService.findConfAttributeSelectionKeyMap();
		List<ConfAttributeSelection> confAttributeSelectionList = new ArrayList<ConfAttributeSelection>();
		int tempSelect=0;
		tempConfAttributeSelection = confAttributeSelections.get(ConfAttributeConstant.KEY_USE_PRE_KEY_BLACK);
		if(tempConfAttributeSelection!=null){
			tempSelect = tempConfAttributeSelection.getConfValue();
			if(usePreKeyUse==1){
				tempConfAttributeSelection.setConfValue(1); 
			}else{
				tempConfAttributeSelection.setConfValue(0);
			}
			if(tempSelect!=tempConfAttributeSelection.getConfValue()){
				tempConfAttributeSelection.setConfUpdateTime(new Date());
			}
			confAttributeSelectionList.add(tempConfAttributeSelection);
		}
		tempConfAttributeSelection = confAttributeSelections.get(ConfAttributeConstant.KEY_USE_PRE_KEY_WHITE);
		if(tempConfAttributeSelection!=null){
			tempSelect = tempConfAttributeSelection.getConfValue();
			if(usePreKeyUse==2){
				tempConfAttributeSelection.setConfValue(1); 
			}else{
				tempConfAttributeSelection.setConfValue(0);
			}
			if(tempSelect!=tempConfAttributeSelection.getConfValue()){
				tempConfAttributeSelection.setConfUpdateTime(new Date());
			}
			confAttributeSelectionList.add(tempConfAttributeSelection);
		}
		tempConfAttributeSelection = confAttributeSelections.get(ConfAttributeConstant.KEY_NO_USE_PRE_KEY);
		if(tempConfAttributeSelection!=null){
			tempSelect = tempConfAttributeSelection.getConfValue();
			if(usePreKeyUse==3){
				tempConfAttributeSelection.setConfValue(1); 
			}else{
				tempConfAttributeSelection.setConfValue(0);
			}
			if(tempSelect!=tempConfAttributeSelection.getConfValue()){
				tempConfAttributeSelection.setConfUpdateTime(new Date());
			}
			confAttributeSelectionList.add(tempConfAttributeSelection);
		}
		tempConfAttributeSelection = confAttributeSelections.get(ConfAttributeConstant.KEY_USE_POST_KEY_BLACK);
		if(tempConfAttributeSelection!=null){
			tempSelect = tempConfAttributeSelection.getConfValue();
			if(usePostKeyUse==1){
				tempConfAttributeSelection.setConfValue(1); 
			}else{
				tempConfAttributeSelection.setConfValue(0);
			}
			if(tempSelect!=tempConfAttributeSelection.getConfValue()){
				tempConfAttributeSelection.setConfUpdateTime(new Date());
			}
			confAttributeSelectionList.add(tempConfAttributeSelection);
		}
		tempConfAttributeSelection = confAttributeSelections.get(ConfAttributeConstant.KEY_USE_POST_KEY_WHITE);
		if(tempConfAttributeSelection!=null){
			tempSelect = tempConfAttributeSelection.getConfValue();
			if(usePostKeyUse==2){
				tempConfAttributeSelection.setConfValue(1); 
			}else{
				tempConfAttributeSelection.setConfValue(0);
			}
			if(tempSelect!=tempConfAttributeSelection.getConfValue()){
				tempConfAttributeSelection.setConfUpdateTime(new Date());
			}
			confAttributeSelectionList.add(tempConfAttributeSelection);
		}
		tempConfAttributeSelection = confAttributeSelections.get(ConfAttributeConstant.KEY_NO_USE_POST_KEY);
		if(tempConfAttributeSelection!=null){
			tempSelect = tempConfAttributeSelection.getConfValue();
			if(usePostKeyUse==3){
				tempConfAttributeSelection.setConfValue(1); 
			}else{
				tempConfAttributeSelection.setConfValue(0);
			}
			if(tempSelect!=tempConfAttributeSelection.getConfValue()){
				tempConfAttributeSelection.setConfUpdateTime(new Date());
			}
			confAttributeSelectionList.add(tempConfAttributeSelection);
		}
		
		tempConfAttributeSelection = confAttributeSelections.get(ConfAttributeConstant.KEY_USE_POST_DOMAIN_BLACK);
		if(tempConfAttributeSelection!=null){
			tempSelect = tempConfAttributeSelection.getConfValue();
			if(usePostDomainUse==1){
				tempConfAttributeSelection.setConfValue(1); 
			}else{
				tempConfAttributeSelection.setConfValue(0);
			}
			if(tempSelect!=tempConfAttributeSelection.getConfValue()){
				tempConfAttributeSelection.setConfUpdateTime(new Date());
			}
			confAttributeSelectionList.add(tempConfAttributeSelection);
		}
		tempConfAttributeSelection = confAttributeSelections.get(ConfAttributeConstant.KEY_USE_POST_DOMAIN_WHITE);
		if(tempConfAttributeSelection!=null){
			tempSelect = tempConfAttributeSelection.getConfValue();
			if(usePostDomainUse==2){
				tempConfAttributeSelection.setConfValue(1); 
			}else{
				tempConfAttributeSelection.setConfValue(0);
			}
			if(tempSelect!=tempConfAttributeSelection.getConfValue()){
				tempConfAttributeSelection.setConfUpdateTime(new Date());
			}
			confAttributeSelectionList.add(tempConfAttributeSelection);
		}
		tempConfAttributeSelection = confAttributeSelections.get(ConfAttributeConstant.KEY_NO_USE_POST_DOMAIN);
		if(tempConfAttributeSelection!=null){
			tempSelect = tempConfAttributeSelection.getConfValue();
			if(usePostDomainUse==3){
				tempConfAttributeSelection.setConfValue(1); 
			}else{
				tempConfAttributeSelection.setConfValue(0);
			}
			if(tempSelect!=tempConfAttributeSelection.getConfValue()){
				tempConfAttributeSelection.setConfUpdateTime(new Date());
			}
			confAttributeSelectionList.add(tempConfAttributeSelection);
		}
		
		tempConfAttributeSelection = confAttributeSelections.get(ConfAttributeConstant.KEY_EPG_EXPIRE_DAY);
		if(tempConfAttributeSelection!=null){
			tempSelect = tempConfAttributeSelection.getConfValue();
			if(epgExpireDay!=null&&!"".equals(epgExpireDay)){
				tempConfAttributeSelection.setConfValue(epgExpireDay); 
			}
			if(tempSelect!=tempConfAttributeSelection.getConfValue()){
				tempConfAttributeSelection.setConfUpdateTime(new Date());
			}
			confAttributeSelectionList.add(tempConfAttributeSelection);
		}
		
		tempConfAttributeSelection = confAttributeSelections.get(ConfAttributeConstant.KEY_USE_BAIDU_API);
		if(tempConfAttributeSelection!=null){
			tempSelect = tempConfAttributeSelection.getConfValue();
			if(apiSearchUse==1){
				tempConfAttributeSelection.setConfValue(1); 
			}else{
				tempConfAttributeSelection.setConfValue(0);
			}
			if(tempSelect!=tempConfAttributeSelection.getConfValue()){
				tempConfAttributeSelection.setConfUpdateTime(new Date());
			}
			confAttributeSelectionList.add(tempConfAttributeSelection);
		}
		
		tempConfAttributeSelection = confAttributeSelections.get(ConfAttributeConstant.KEY_USE_GOOGLE_API);
		if(tempConfAttributeSelection!=null){
			tempSelect = tempConfAttributeSelection.getConfValue();
			if(apiSearchUse==2){
				tempConfAttributeSelection.setConfValue(1); 
			}else{
				tempConfAttributeSelection.setConfValue(0); 
			}
			if(tempSelect!=tempConfAttributeSelection.getConfValue()){
				tempConfAttributeSelection.setConfUpdateTime(new Date());
			}
			confAttributeSelectionList.add(tempConfAttributeSelection);
		}
		tempConfAttributeSelection = confAttributeSelections.get(ConfAttributeConstant.KEY_USE_YAHOO_API);
		if(tempConfAttributeSelection!=null){
			tempSelect = tempConfAttributeSelection.getConfValue();
			if(apiSearchUse==3){
				tempConfAttributeSelection.setConfValue(1); 
			}else{
				tempConfAttributeSelection.setConfValue(0); 
			}
			if(tempSelect!=tempConfAttributeSelection.getConfValue()){
				tempConfAttributeSelection.setConfUpdateTime(new Date());
			}
			confAttributeSelectionList.add(tempConfAttributeSelection);
		}
		tempConfAttributeSelection = confAttributeSelections.get(ConfAttributeConstant.KEY_USE_SINA_API);
		if(tempConfAttributeSelection!=null){
			tempSelect = tempConfAttributeSelection.getConfValue();
			if(twitterApiSearchUse==1){
				tempConfAttributeSelection.setConfValue(1); 
			}else{
				tempConfAttributeSelection.setConfValue(0);
			}
			if(tempSelect!=tempConfAttributeSelection.getConfValue()){
				tempConfAttributeSelection.setConfUpdateTime(new Date());
			}
			confAttributeSelectionList.add(tempConfAttributeSelection);
		}
		tempConfAttributeSelection = confAttributeSelections.get(ConfAttributeConstant.KEY_USE_TWITTER_API);
		if(tempConfAttributeSelection!=null){
			tempSelect = tempConfAttributeSelection.getConfValue();
			if(twitterApiSearchUse==2){
				tempConfAttributeSelection.setConfValue(1); 
			}else{
				tempConfAttributeSelection.setConfValue(0);
			}
			if(tempSelect!=tempConfAttributeSelection.getConfValue()){
				tempConfAttributeSelection.setConfUpdateTime(new Date());
			}
			confAttributeSelectionList.add(tempConfAttributeSelection);
		}
        /**do your update logics**/
//		if(!oldConfName.equals(confAttributeSelection.getConfName())){
//			if(confAttributeSelectionService.isExistConfAttributeSelectionName(confAttributeSelection.getConfName())){
//				this.pageState = ViewConstant.EDITSTATUS;
//				super.setErrorInfo(getText("info.modifyFailed"));
//				super.setErrorInfo(getText("cep.confAttributeSelection.confName.duplicate"));
//				return ViewConstant.ADDINPUT;
//			}
//		}
//		confAttributeSelectionService.saveOrUpdateConfAttributeSelection(confAttributeSelection);
		if(!confAttributeSelectionList.isEmpty()){
			confAttributeSelectionService.saveAllConfAttributeSelections(confAttributeSelectionList);
		}
		setSuccessinfo(getText("info.addSuccess"));
		super.doReQuery();
		return ViewConstant.ADDINPUT;
	}

	public String doBatchDelete() throws Exception {
		confAttributeSelectionService.batchDeleteConfAttributeSelectionByIds(ids);
		setSuccessinfo(getText("info.deleteSuccess"));
		return super.doQuery();
	}


	public String doDelete() throws Exception {
		confAttributeSelectionService.deleteConfAttributeSelectionById(id);
		return super.doQuery();
	}



	// actions end

	// getters and setters begin



	public ConfAttributeSelection getConfAttributeSelection() {
		return this.confAttributeSelection;
	}

	public void setConfAttributeSelection (ConfAttributeSelection confAttributeSelection) {
		this.confAttributeSelection = confAttributeSelection;
	}


	public void setConfAttributeSelectionService(ConfAttributeSelectionService confAttributeSelectionService) {
		this.confAttributeSelectionService = confAttributeSelectionService;
	}
	
	
	public String getQ_confName() {
		return this.q_confName;
	}

	public void setQ_confName(String confName) {
		this.q_confName = confName;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getQ_confDescription() {
		return q_confDescription;
	}

	public void setQ_confDescription(String description) {
		q_confDescription = description;
	}

	public String getQ_confKey() {
		return q_confKey;
	}

	public void setQ_confKey(String key) {
		q_confKey = key;
	}

	public String getQ_confValue() {
		return q_confValue;
	}

	public void setQ_confValue(String value) {
		q_confValue = value;
	}

	public Integer getApiSearchUse() {
		return apiSearchUse;
	}

	public void setApiSearchUse(Integer apiSearchUse) {
		this.apiSearchUse = apiSearchUse;
	}

	public Integer getTwitterApiSearchUse() {
		return twitterApiSearchUse;
	}

	public void setTwitterApiSearchUse(Integer twitterApiSearchUse) {
		this.twitterApiSearchUse = twitterApiSearchUse;
	}


	public Integer getUsePreKeyUse() {
		return usePreKeyUse;
	}

	public void setUsePreKeyUse(Integer usePreKeyUse) {
		this.usePreKeyUse = usePreKeyUse;
	}

	public Integer getUsePostKeyUse() {
		return usePostKeyUse;
	}

	public void setUsePostKeyUse(Integer usePostKeyUse) {
		this.usePostKeyUse = usePostKeyUse;
	}

	public Integer getUsePostDomainUse() {
		return usePostDomainUse;
	}

	public void setUsePostDomainUse(Integer usePostDomainUse) {
		this.usePostDomainUse = usePostDomainUse;
	}

	public void setEpgExpireDay(Integer epgExpireDay) {
		this.epgExpireDay = epgExpireDay;
	}

	public Integer getEpgExpireDay() {
		return epgExpireDay;
	}




	// getters and setters end
}